package com.gnerv.sylvanas.business.manage.logic;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.framework.core.BaseUkIdGenerate;
import com.gnerv.sylvanas.business.manage.model.bo.OrgBO;
import com.gnerv.sylvanas.business.manage.model.dto.OrgAccountDTO;
import com.gnerv.sylvanas.business.manage.model.dto.OrgCreateDTO;
import com.gnerv.sylvanas.business.manage.model.dto.OrgDTO;
import com.gnerv.sylvanas.business.manage.model.dto.OrgTreeDTO;
import com.gnerv.sylvanas.business.manage.model.dto.OrgUpdateDTO;
import com.gnerv.sylvanas.business.manage.operation.OrgOperationService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class OrgLogicService  {

    private final OrgOperationService orgOperationService;
    private final BaseUkIdGenerate baseUkIdGenerate;

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(OrgCreateDTO orgCreateDTO) {
        OrgBO orgBO = orgCreateDTO.toOrgBO();
        orgBO.setUkId(baseUkIdGenerate.generate());
        orgOperationService.create(orgBO);
        return RestResult.ok();
    }

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        orgOperationService.deleteByUkId(ukId);
        orgOperationService.unbindAccount(ukId);
        return RestResult.ok();
    }

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(OrgUpdateDTO orgUpdateDTO) {
        OrgBO orgBO = orgUpdateDTO.toOrgBO();
        orgOperationService.updateByUkId(orgBO);
        return RestResult.ok();
    }

    
    @Transactional(readOnly = true)
    public RestResult<OrgDTO> selectByUkId(String ukId) {
        return RestResult.ok(orgOperationService.selectByUkId(ukId));
    }

    
    @Transactional(readOnly = true)
    public RestResult<List<OrgDTO>> selectListByName(String name) {
        return RestResult.ok(orgOperationService.selectListByName(name));
    }

    
    @Transactional(readOnly = true)
    public RestResult<List<OrgDTO>> selectListByParentUkId(String parentUkId) {
        List<OrgDTO> orgList = orgOperationService.selectListByParentUkId(parentUkId);
        return RestResult.ok(orgList);
    }

    
    @Transactional(readOnly = true)
    public RestResult<List<OrgTreeDTO>> selectListTreeByParentUkId(String parentUkId) {
        return RestResult.ok(orgOperationService.selectListTreeByParentUkId(parentUkId));
    }

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> bindAccount(OrgAccountDTO orgAccountDTO) {
        orgOperationService.unbindAccount(orgAccountDTO.getOrgUkId());
        orgOperationService.bindAccount(orgAccountDTO.getOrgUkId(), orgAccountDTO.getAccountUkIdList());
        return RestResult.ok();
    }

}
